package com.shundr.common.util;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;

import com.shundr.common.dao.AreaDao;
import com.shundr.common.model.AreaInfo;
import com.shundr.frame.constant.LContext;
import com.shundr.frame.util.LLogger;

public class AreaUtils {
	
	public static List<AreaInfo> getCitiesFromAssets(Context context){
		List<AreaInfo> list = new ArrayList<AreaInfo>();
		try {
			String json = getAssetString(context,"city.json");
			JSONObject object = new JSONObject(json);
			Iterator iterator = object.keys();
			while(iterator.hasNext()){
				String parentCode = iterator.next().toString();
				JSONArray array = new JSONArray(object.get(parentCode).toString());
				for(int i = 0;i < array.length(); i ++){
					AreaInfo info = new AreaInfo();
					info.setParentCode(parentCode);
					JSONObject subObject = new JSONObject(array.get(i).toString());
					Iterator subIter = subObject.keys();
					while(subIter.hasNext()){
						String code = subIter.next().toString();
						String value = subObject.getString(code);
						info.setCode(code);
						info.setName(value);
						list.add(info);
					}
				}
			}
			LLogger.e("larry","size : " + list.size());
			new AreaDao().syncData(list);
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return list;
	}
	
	/**
	 * 获省市数据并入库
	 * getCitiesFromAssets(这里用一句话描述这个方法的作用)
	 * (这里描述这个方法适用条件 – 可选)
	 * @param context
	 * @return
	 * List<AreaInfo>
	 * @exception
	 * @since  1.0.0
	 */
	public static List<AreaInfo> getCitiesFromAssets_1(Context context){
		List<AreaInfo> list = new ArrayList<AreaInfo>();
		try {
			String json = getAssetString(context,"city_new.json");
//			LLogger.e("larry","json : " + json);
			List<AreaInfo> list_1 = JsonUtil.getList(json, AreaInfo.class);
			list.addAll(list_1);
			LLogger.e("larry","size : " + list.size());
			new AreaDao().syncData(list);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return list;
	}
	
	/**
	 * 获取assets下文件的string
	 * getAssetString(这里用一句话描述这个方法的作用)
	 * (这里描述这个方法适用条件 – 可选)
	 * @param context
	 * @param fileName
	 * @return
	 * String
	 * @exception
	 * @since  1.0.0
	 */
	public static String getAssetString(Context context,String fileName) {  
        BufferedReader bufferedReader = null;  
        try {  
            bufferedReader = new BufferedReader(new InputStreamReader(context.getAssets().open(fileName),"utf-8"));  
            String line = null;  
            StringBuilder builder = new StringBuilder();  
            while (null != (line = bufferedReader.readLine())) {  
                builder.append(line).append("\n");  
            }  
            return builder.toString();  
        } catch (IOException e) {  
            e.printStackTrace();  
        }finally{  
            if (null != bufferedReader) {  
                try {  
                    bufferedReader.close();  
                } catch (IOException e) {  
                    e.printStackTrace();  
                }  
            }  
            bufferedReader = null;  
        }  
        return "";  
    } 
	
	/**
	 * 根据省市获取定位code
	 * getAreaCode(这里用一句话描述这个方法的作用)
	 * (这里描述这个方法适用条件 – 可选)
	 * @param p
	 * @param c
	 * @return
	 * String
	 * @exception
	 * @since  1.0.0
	 */
	public static AreaInfo getAreaInfo(String p, String c, String d) {
		try {
			AreaDao areaDao = new AreaDao();
			List<AreaInfo> provinces = areaDao.getAreasByParent("00");
			AreaInfo province = null;
			for (AreaInfo info : provinces) {
				if (info.getName().contains(p)) {
					province = info;
				}
			}
			List<AreaInfo> cities = areaDao.getAreasByParent(province.getCode());
			AreaInfo city = null;
			for (AreaInfo info : cities) {
				if (info.getName().contains(c)) {
					city = info;
				}
			}
			if (city == null) {
				LContext.areaInfo = province;
				return province;
			}
			List<AreaInfo> districts = areaDao.getAreasByParent(city.getCode());
			AreaInfo district = null;
			for (AreaInfo info : districts) {
				if (info.getName().contains(d)) {
					district = info;
				}
			}
			if (district == null) {
				LContext.areaInfo = city;
				return city;
			}
			LContext.areaInfo = district;
			return district;
		} catch (Exception e) {
			e.printStackTrace();
			AreaInfo nullInfo = new AreaInfo();
			nullInfo.setName("全国");
			nullInfo.setCode(LContext.nullStr);
			return nullInfo;
		}
	}
	
	/**
	 * 根据省市获取定位code
	 * getAreaCode(这里用一句话描述这个方法的作用)
	 * (这里描述这个方法适用条件 – 可选)
	 * @param p
	 * @param c
	 * @return
	 * String
	 * @exception
	 * @since  1.0.0
	 */
	public static AreaInfo getAreaInfo(String p,String c){
		try {
			AreaDao areaDao = new AreaDao();
			List<AreaInfo> provinces = areaDao.getAreasByParent("00");
			AreaInfo province = null;
			for(AreaInfo info : provinces){
				if(info.getName().contains(p)){
					province = info;
				}
			}
			List<AreaInfo> cities = areaDao.getAreasByParent(province.getCode());
			AreaInfo city = null;
			for(AreaInfo info : cities){
				if(info.getName().contains(c)){
					city = info;
				}
			}
			if(city == null){
				LContext.areaInfo = province;
				return province;
			}
			LContext.areaInfo = city;
			return city;
		} catch (Exception e) {
			e.printStackTrace();
			AreaInfo  nullInfo = new AreaInfo();
			nullInfo.setName("全国");
			nullInfo.setCode(LContext.nullStr);
			return nullInfo;
		}
	}
}
